<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>用户注册与登录</title>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/crypto-js/4.0.0/crypto-js.min.js"></script>
    <style>
        body {
            font-family: Arial, sans-serif;
            display: flex;
            justify-content: center;
            align-items: center;
            min-height: 100vh;
            margin: 0;
            background-color: #f4f4f4;
        }
     .container {
            background-color: #fff;
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
        }
     .form-group {
            margin-bottom: 15px;
        }
     .form-group label {
            display: block;
            margin-bottom: 5px;
        }
     .form-group input {
            width: 100%;
            padding: 8px;
            border: 1px solid #ccc;
            border-radius: 3px;
        }
     .btn {
            padding: 10px 20px;
            background-color: #007BFF;
            color: white;
            border: none;
            border-radius: 3px;
            cursor: pointer;
        }
     .btn:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
    <div class="container">
        <h2>用户注册</h2>
        <form id="register-form">
            <div class="form-group">
                <label for="register-username">用户名</label>
                <input type="text" id="register-username" name="username" required>
            </div>
            <div class="form-group">
                <label for="register-password">密码</label>
                <input type="password" id="register-password" name="password" required>
            </div>
            <button type="submit" class="btn">注册</button>
        </form>
        <h2>用户登录</h2>
        <form id="login-form">
            <div class="form-group">
                <label for="login-username">用户名</label>
                <input type="text" id="login-username" name="username" required>
            </div>
            <div class="form-group">
                <label for="login-password">密码</label>
                <input type="password" id="login-password" name="password" required>
            </div>
            <button type="submit" class="btn">登录</button>
        </form>
    </div>
    <script>
        const encryptData = (data, key) => {
            return CryptoJS.AES.encrypt(JSON.stringify(data), key).toString();
        };

        document.getElementById('register-form').addEventListener('submit', function (event) {
            event.preventDefault();
            const username = document.getElementById('register-username').value;
            const password = document.getElementById('register-password').value;
            const key = 'yourSecretKey12345'; // 请替换为你的密钥
            const encryptedData = encryptData({ username, password }, key);

            fetch('http:localhost:5000/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ encryptedData })
            })
          .then(response => response.json())
          .then(data => {
                    alert(data.message);
                });
        });

        document.getElementById('login-form').addEventListener('submit', function (event) {
            event.preventDefault();
            const username = document.getElementById('login-username').value;
            const password = document.getElementById('login-password').value;
            const key = 'yourSecretKey12345'; // 请替换为你的密钥
            const encryptedData = encryptData({ username, password }, key);

            fetch('http:localhost:5000/login', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({ encryptedData })
            })
          .then(response => response.json())
          .then(data => {
                    alert(data.message);
                });
        });
    </script>
</body>
</html>